package com.wwwzh.himalayafm.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wwwzh.himalayafm.R;
import com.wwwzh.himalayafm.adapter.DisPagerAdapter;
import com.wwwzh.himalayafm.fragment.disfragment.AnchorFragment;
import com.wwwzh.himalayafm.fragment.disfragment.CategoryFragment;
import com.wwwzh.himalayafm.fragment.disfragment.LiveFragment;
import com.wwwzh.himalayafm.fragment.disfragment.RankingFragment;
import com.wwwzh.himalayafm.fragment.disfragment.recommend.RecommendFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * 发现碎片
 */
public class DiscoveryFragment extends Fragment implements ViewPager.OnPageChangeListener {


    private ViewPager viewPager;
    private TabLayout tabLayout;
    private List<String> titleList;
    private List<Fragment> fragList;
    private DisPagerAdapter adapter;

    public DiscoveryFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_discovery, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = (ViewPager) view.findViewById(R.id.dis_viewPager);
        tabLayout = (TabLayout) view.findViewById(R.id.dis_tab);
        initDisFragment();
        adapter = new DisPagerAdapter(getChildFragmentManager(), fragList, titleList);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(0);
        viewPager.addOnPageChangeListener(this);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setCurrentItem(0);
    }

    //初始化碎片集合和标题集合
    private void initDisFragment() {
        titleList = new ArrayList<>();
        titleList.add("推荐");
        titleList.add("分类");
        titleList.add("广播");
        titleList.add("榜单");
        titleList.add("直播");
        fragList = new ArrayList<>();
        fragList.add(new RecommendFragment());
        fragList.add(new CategoryFragment());
        fragList.add(new LiveFragment());
        fragList.add(new RankingFragment());
        fragList.add(new AnchorFragment());
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        switch (position) {
            case 0:
            case 1:
                RankingFragment rankingFragment = (RankingFragment) fragList.get(3);
                RecyclerView rankRecycle = rankingFragment.getRecylerView();
                if (rankRecycle != null) {
                    rankRecycle.scrollToPosition(0);
                }
                break;

    }


}

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
